/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.StateDAL;
import mnstudent.datatranferobject.StateDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author 11520_000
 */
public class StateBLL {
    
    private StateDAL m_StateDAL;
    
    public StateBLL()
    {
        this.m_StateDAL = new StateDAL();
    }
   
     //<editor-fold defaultstate="collapsed" desc="Method">
    
     /**
     * Ham lay tat ca cac khoa
     *
     * @return ResultSet
     */
    public ResultSet getAllState(){
        
        return m_StateDAL.getAllState();  
    }
    public ArrayList<StateDTO> getAllStateToArray()
    {
        ArrayList<StateDTO> temp = new ArrayList<>();
        ResultSet rs = getAllState();
        try {
            while(rs.next())
            {
                StateDTO state = new StateDTO(rs.getNString(1), rs.getNString(2));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getStateById(String _id)
    {
       return m_StateDAL.getStateById(_id);
    }
    
     /**
     * ham them moi khoa xuong database
     * @param _state
     * @return true: success, false: fail
     */
    public boolean insertStateToDatabase(StateDTO _state)
    {
        
        return m_StateDAL.insertStateToDatabase(_state);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _faculty
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateStateToDatabase(StateDTO _state)
    {
       return m_StateDAL.updateStateToDatabase(_state);
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _faculty
     * @return 
     */
     public boolean deleteStateFromDatabase(StateDTO _state)
    {
        return m_StateDAL.deleteStateFromDatabase(_state);
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
    //</editor-fold>
}
